package com.scada.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.IUserDao;
import com.scada.dao.IUserRoleDao;
import com.scada.inf.UserVisitor;
import com.scada.model.bean.User;
import com.scada.model.bean.UserRole;
import com.scada.model.bean.vo.UserRoleVO;
import com.scada.model.bean.vo.UserVO;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;

@Service("userVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class UserVisitorImpl implements UserVisitor{

	@Autowired
	private IUserDao iUserDao;
	
	@Autowired
	private IUserRoleDao iUserRoleDao;
	
	@Override
	@Transactional
	public ResponseBean insert(User user,String roleIds){
		ResponseBean result = new ResponseBean();
		try {
			user.setState(User.USER_STATE.USER_STATE_ABLED.getKey());
			iUserDao.insertOneUser(user);//插入返回id
			List<UserRole> urli = new ArrayList<UserRole>();
			String[] str = roleIds.split(",");
			UserRole ur = null;
			if(str.length > 0){
				for(String r : str){
					ur = new UserRole();
					ur.setSysRoleId(Integer.parseInt(r));
					ur.setUserId(user.getId());
					ur.setState(UserRole.USER_ROLE_STATE.USER_ROLE_STATE_SHOW.getKey());
					urli.add(ur);
				}
				iUserRoleDao.insertSelective(urli);
			}
			result.success("新增成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("新增异常");
		}
		return result;
	}
	
	@Override
	@Transactional
	public ResponseBean update(User user,String roleIds) {
		ResponseBean result = new ResponseBean();
		try {
			iUserDao.updateUser(user);
			//删除所有角色数据，重新添加、
			List<Integer> li = new ArrayList<Integer>();
			li.add(user.getId());
			iUserRoleDao.deleteByPrimaryKey(li);
			List<UserRole> urli = new ArrayList<UserRole>();
			String[] str = roleIds.split(",");
			UserRole ur = null;
			for(String r : str){
				ur = new UserRole();
				ur.setSysRoleId(Integer.parseInt(r));
				ur.setUserId(user.getId());
				urli.add(ur);
			}
			iUserRoleDao.insertSelective(urli);
			result.success("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return result;
	}
	
	
	@Override
	public User findUserByLoginName(Map<String,Object> map) {
		User u = iUserDao.findUserForLogin(map);
		return u;
	}
	
	@Override
	public UserRoleVO findUserDetail(int id) {
		
		return iUserDao.findUserDetail(id);
	}

	@Override
	public PageInfo<UserVO> findDataList(PageInfo<UserVO> pageInfo) {
		User user = (User)pageInfo.getCondition();
		int count = iUserDao.count(user);
		pageInfo.setRecordsTotal(count);
		List<UserVO> uli = iUserDao.findDataList(pageInfo);
		pageInfo.setData(uli);
		return pageInfo;
	}

	@Override
	@Transactional
	public ResponseBean updateState(String uIds, int state, 
			String method) {
		
		ResponseBean result= new ResponseBean();
		Map<String ,Object> map = new HashMap<String, Object>();
		String[] str = uIds.split(",");
		List<Integer> ls = new ArrayList<Integer>();
		for(String s : str){
			ls.add(Integer.parseInt(s));
		}
		map.put("state", state);
		map.put("uIds", ls);
		try {
			if(method.equals("delete")){
				iUserDao.deleteByPrimaryKey(map);
				iUserRoleDao.deleteByPrimaryKey(ls);
				result.success("删除成功");
			}else{
				iUserDao.deleteByPrimaryKey(map);
				result.success("操作成功");
			}
		} catch (Exception e) {
			result.failed("操作失败");
			e.printStackTrace();
		}
		return result;
	}
	
	
	

	@Override
	public ResponseBean updatePassword(String newPad, Integer id) {
		
		ResponseBean result= new ResponseBean();
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", id);
			map.put("password", DigestUtils.md5Hex(newPad));
			iUserDao.updatePassword(map);
			result.success();
		}catch(Exception e){
			
		}
		return result;
	}

	@Override
	public ResponseBean updateLoginDate(User user) {
		
		ResponseBean result= new ResponseBean();
		try{
			iUserDao.updateUser(user);
			result.success("修改成功");
		}catch(Exception e){
			result.failed("修改失败");
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public ResponseBean usernameExist(String loginName, String userId) {
		
		ResponseBean result= new ResponseBean();
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("loginName", loginName);
		map.put("userId", userId);
		try{
			result.addObject("result", iUserDao.usernameExist(map));
			result.success();
		}catch(Exception e){
			result.failed();
			e.printStackTrace();
		}
		return result;
	}

}
